package field;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

public class View {
    private Board board;

    private JFrame frame = new JFrame();
    private static int frameWidth;
    private static int frameHeight;

    private static final int BUTTON_DISTANCE = 60;
    private static final int BUTTON_X = 640;
    private static final int BUTTON_Y = 30;
    private static final int BUTTON_WIDTH = 80;
    private static final int BUTTON_HEIGHT = 30;
    private ArrayList<JButton> buttons = new ArrayList<>();

    private static JLabel prompt = new JLabel("It is Black's turn.");
    private static final int LABEL_WIDTH = 100;
    private static final int LABEL_HEIGHT = 30;

    public View(Board board) {
        this.board = board;

        initButtons();
        initLabel();
        initBoard();
        initFrame();
    }

    public JButton getButton(int index) {
        return buttons.get(index);
    }

    public JFrame getFrame() {
        return frame;
    }

    /**
     * display the whole game frame.
     */
    public void display() {
        //frame.pack();
        frame.repaint();
    }

    /**
     * set the prompt label's text
     */
    public void setPrompt(boolean isBlack) {
        if (isBlack) {
            prompt.setText("It is Black's turn.");
        } else {
            prompt.setText("It is White's turn.");
        }
    }

    public void setPromptVisible(boolean flag) {
            prompt.setVisible(flag);
    }

    /**
     * create three buttons, set their size, position and function.
     */
    public void initButtons() {
        JButton start = new JButton("New");
        JButton undo = new JButton("Undo");
        JButton about = new JButton("About");

        buttons.add(start);
        buttons.add(undo);
        buttons.add(about);
        for (int i = 0; i < buttons.size(); ++i) {
            buttons.get(i).setSize(BUTTON_WIDTH, BUTTON_HEIGHT);
            buttons.get(i).setBounds(BUTTON_X, BUTTON_Y + i * BUTTON_DISTANCE, BUTTON_WIDTH, BUTTON_HEIGHT);
            frame.add(buttons.get(i));
        }
    }

    /**
     * set the frame to center and some properties.
     */
    public void initFrame() {
        frameWidth = board.width + 150;
        frameHeight = board.height + 24;
        frame.setSize(frameWidth, frameHeight);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        frame.setLocation((screenSize.width - frameWidth) / 2,
                (screenSize.height - frameHeight) / 2);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setTitle("Five-In-Row");
        frame.setVisible(true);
    }

    /**
     * set the label's size and location.
     */
    public void initLabel() {
        prompt.setSize(LABEL_WIDTH, LABEL_HEIGHT);
        prompt.setBounds(board.width + 25, BUTTON_Y + 4 * BUTTON_DISTANCE, LABEL_WIDTH, LABEL_HEIGHT);
        frame.add(prompt);
    }

    public void initBoard() {
        board.setBounds(0, 0, board.width, board.height);
        frame.add(board);
    }
}